package socketio.server;

import com.corundumstudio.socketio.*;
import com.corundumstudio.socketio.listener.DataListener;

import java.util.Collection;

/**
 * Created by alex on 16-1-13.
 */
public class NamespaceChatLauncher {

    public static void main(String[] args) throws InterruptedException {

        Configuration configuration = new Configuration();
        configuration.setHostname("192.168.1.65");
        configuration.setPort(9092);
        configuration.setOrigin("http://192.168.1.65:3000");

        final SocketIOServer server = new SocketIOServer(configuration);

        final SocketIONamespace chat1Namespace = server.addNamespace("/chat1");
        chat1Namespace.addEventListener("message", ChatObject.class, new DataListener<ChatObject>() {
            @Override
            public void onData(SocketIOClient client, ChatObject data, AckRequest ackSender) throws Exception {
                //broadcast message to all clients
                chat1Namespace.getBroadcastOperations().sendEvent("message", data);
            }
        });

        final SocketIONamespace chat2namespace = server.addNamespace("/chat2");
        chat2namespace.addEventListener("message", ChatObject.class, new DataListener<ChatObject>() {
            @Override
            public void onData(SocketIOClient client, ChatObject data, AckRequest ackSender) throws Exception {
                //broadcast message to all clients
                chat2namespace.getBroadcastOperations().sendEvent("message", data);
                server.getClient(client.getSessionId()).sendEvent("private", "hello client");
                Collection<SocketIONamespace> allNamespaces = server.getAllNamespaces();
            }
        });

        server.start();
        Thread.sleep(Integer.MAX_VALUE);
        server.stop();
    }
}
